﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ProductImagesBL
    {
        public void Insert(tbl_ProductImage productImg)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductImage newproductImg = new tbl_ProductImage()
                    {
                        Status=productImg.Status,
                        ProductNo=productImg.ProductNo,
                        IndexShow=productImg.IndexShow,
                        ImgSmall=productImg.ImgSmall,
                        ImgReview=productImg.ImgReview,
                        ImgMedium=productImg.ImgMedium,
                        ImgLarge=productImg.ImgLarge,
                        Description=productImg.Description,
                        CreatedUser=productImg.CreatedUser,
                        CreatedDate=productImg.CreatedDate
                    };
                    contextdb.tbl_ProductImages.InsertOnSubmit(newproductImg);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductImage productImg)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproductImg = contextdb.tbl_ProductImages.SingleOrDefault(u => u.ImageNo == productImg.ImageNo);
                try
                {
                    if (updateproductImg != null)
                    {
                        updateproductImg.Status = productImg.Status;
                        updateproductImg.ProductNo = productImg.ProductNo;
                        updateproductImg.IndexShow = productImg.IndexShow;
                        updateproductImg.ImgSmall = productImg.ImgSmall;
                        updateproductImg.ImgReview = productImg.ImgReview;
                        updateproductImg.ImgMedium = productImg.ImgMedium;
                        updateproductImg.ImgLarge = productImg.ImgLarge;
                        updateproductImg.Description = productImg.Description;
                        updateproductImg.CreatedUser = productImg.CreatedUser;
                        updateproductImg.CreatedDate = productImg.CreatedDate;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int productImgNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductImgNo = contextdb.tbl_ProductImages.SingleOrDefault(u => u.ImageNo == productImgNo);
                try
                {
                    if (deleteproductImgNo != null)
                    {
                        contextdb.tbl_ProductImages.DeleteOnSubmit(deleteproductImgNo);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int productImgNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductImgNo = contextdb.tbl_ProductImages.SingleOrDefault(u => u.ImageNo == productImgNo);
                try
                {
                    if (deleteproductImgNo != null)
                    {
                        deleteproductImgNo.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}
